function getBoxLayer(): MainModal {
	return MainWindow.ins().modal;
}

// ok 1 ,cancel 0
type msgboxCallback = (rt: number | string) => void;

namespace ui {
	export function msgbox(text: string, func: msgboxCallback) {
		// msgline(Text);
		const box = new DlgMsgbox(text, func);
		box.popup();
		return box;
	}

	export function msgboxCheck(text: string, check: string, func: msgboxCallback) {
		// msgline(Text);
		const box = new DlgMsgboxCheck(text, check, func);
		box.popup();
		return box;
	}

	export function msgboxParam(param: MsgBoxParam | string, func: msgboxCallback) {
		let msg: string;
		if (typeof param === 'string') {
			msg = param;
		} else {
			msg = Utils.format(param.msg, param.format);
			if (param.check) {
				return msgboxCheck(msg, param.check, func);
			}
		}
		return msgbox(msg, func);
	}

	export async function msgboxParamPromise(param: MsgBoxParam | string): Promise<number> {
		return promiseCallback(msgboxParam)(param);
	}

	export function msginput(text: string, input: string, func: msgboxCallback): void {
		// msgline(Text);
		const box = new DlgMsginput(text, input, func);
		box.popup();
	}

	export function msginputParam(param: UIInputParam, func: msgboxCallback): void {
		const msg = Utils.format(param.msg, param.format);
		msginput(msg, param.input, func);
	}

	export async function msginputParamPromise(param: UIInputParam): Promise<number | string> {
		return promiseCallback(msginputParam)(param);
	}

	export class DlgMsgbase extends PopupPanel {
		constructor(msg: string, func: msgboxCallback) {
			super();
			this.msg = msg;
			this.callback = func;
			allCenter(this);
		}

		ok: eui.Button;

		cancel: eui.Button;

		ti: TextWithImage;

		msg: string;

		callback: msgboxCallback;

		override childrenCreated(): void {
			super.childrenCreated();
			this.createTi();
			this.ok.addEventListener('touchTap', this.onOK, this);
			this.cancel.addEventListener('touchTap', this.onCancel, this);
		}

		protected createTi(): void {
			this.ti.text = this.msg;
		}

		onCallback(v: number | string) {
			if (this.callback) {
				this.callback(v);
			}
		}

		override onOK(): void {
			getBoxLayer().removeModal(this, 'popDown');
			this.onCallback(this.getOK());
		}

		override onCancel(): void {
			getBoxLayer().removeModal(this, 'popDown');
			this.onCallback(this.getCancel());
		}

		override onEsc(): void {
			this.onCallback(this.getCancel());
		}

		override async onClose(): Promise<void> {
			this.onCancel();
		}

		getOK(): number | string {
			return 1;
		}

		getCancel(): number | string {
			return 0;
		}

		popup() {
			getBoxLayer().showModal(this, 'popUp');
		}
	}

	export class DlgMsgbox extends DlgMsgbase {
		constructor(msg: string, func: msgboxCallback) {
			super(msg, func);
			this.skinName = 'ui_msgboxSkin';
		}
	}

	export class DlgMsgboxCheck extends DlgMsgbase {
		constructor(msg: string, check: string, func: msgboxCallback) {
			super(msg, func);
			this.check = check;
			this.skinName = 'ui_msgboxcheckSkin';
		}

		override childrenCreated(): void {
			super.childrenCreated();
			this.ck.label = this.check;
		}

		check: string;

		ck: eui.CheckBox;
	}

	export class DlgMsginput extends DlgMsgbase {
		constructor(msg: string, input: string, func: msgboxCallback) {
			super(msg, func);
			this.defaultinput = input;
			this.skinName = 'ui_msginputSkin';
		}

		input: eui.TextInput;

		defaultinput: string;

		override childrenCreated(): void {
			super.childrenCreated();
			if (this.defaultinput) {
				this.input.text = this.defaultinput;
			}
		}

		override getOK(): number | string {
			return this.input.text;
		}

		override getCancel(): number | string {
			return '';
		}
	}

	export class DlgMsgcb extends DlgMsgbase {
		constructor(msg: string, func: msgboxCallback) {
			super(msg, func);
			this.skinName = 'ui_msgcbSkin';
		}

		input: ComboBox;

		override getOK(): number | string {
			return this.input.text;
		}

		override getCancel(): number | string {
			return '';
		}

		settings = '';

		override childrenCreated(): void {
			super.childrenCreated();
			if (this.settings) {
				const data = gamedata.Setting.ins().getValue(this.settings);
				if (data) {
					this.input.values = data;
				}
			}
		}

		override onOK(): void {
			super.onOK();
			this.input.RecordValue();
			if (this.settings) {
				gamedata.Setting.ins().updateKey(this.settings, this.input.values);
			}
		}
	}

	export class DlgConsole extends DlgMsgcb {
		private static instance: DlgConsole;

		public static ins(): DlgConsole {
			if (!this.instance) {
				this.instance = new DlgConsole('请输入客户端命令 editor/console.ts', null);
			}
			return this.instance;
		}

		override settings = 'DlgConsole';

		override childrenCreated(): void {
			super.childrenCreated();
			this.input.text = "echo('client')";
		}

		override onOK(): void {
			super.onOK();
			const input = this.getOK();
			try {
				const msg = eval(input as string);
				ui.log(String(msg));
			} catch (e) {
				console.error(e);
			}
		}
	}

	export class DlgConsoleServer extends DlgMsgcb {
		private static instance: DlgConsoleServer;

		public static ins(): DlgConsoleServer {
			if (!this.instance) {
				this.instance = new DlgConsoleServer(
					'请输入服务器命令 game/module/test/mod_console.ts',
					null,
				);
			}
			return this.instance;
		}

		override settings = 'DlgConsoleServer';

		override childrenCreated(): void {
			super.childrenCreated();
			this.input.text = "echo('server')";
		}

		override async onOK(): Promise<void> {
			super.onOK();
			const input = this.getOK();
			const [rt, msg] = await getPlayerClient().user.asend([MsgId.C2S_TEST_CMD, input]);
			if (rt !== ErrorEnum.ErrorOk) {
				ui.msgErrorEnum(rt);
			} else {
				ui.log(msg as string);
			}
		}
	}
}
